<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>组织机构管理</title>
<script type="text/javascript">
	var $tree;
	$(function() {
		$tree = $("#tree").tree({
			method : "get",
			lines : true,
			url : "${ctx}/fnd/org/tree?sort=orgName&order=asc",
			dataPlain : true, //该属性用以启用当前 easyui-tree 控件对平滑数据格式的支持
			toggleOnClick : false,
			checkbox : true,
			onSelect : function(node) {
				$("#fm").form("clear");
				//alert("测试"+node.id);
				$('#fm').form('load', $.extend(node.attributes, {
					pbsParentNode : node.id
				}));
				/* var ar = splitCode(node.attributes.OrgNodeCode);
				$("#fm").find("input[name=ssCode]").val(ar[0]);
				$("#fm").find("input[name=pbsCode]").val(ar[1]); */
			},
			onContextMenu : function(e, node) {
				e.preventDefault();
				// 查找节点
				$tree.tree('select', node.target);
			},
			contextMenu : [ /* {
				text : "添加子节点",
				iconCls : "icon-standard-add",
				disabled : false,
				handler : function() {
					var snode = $tree.tree("getSelected");
					$tree.tree('expand', snode.target);
					addChild();
				}
			}, {
				text : "添加兄弟节点",
				iconCls : "icon-standard-add",
				disabled : false,
				handler : function() {
					addRoot();
				}
			}, "-",  */{
				text : "全部勾选",
				iconCls : "icon-metro-checkbox-checked",
				disabled : false,
				handler : function() {
					checkAll($tree);
				}
			}, {
				text : "全部取消",
				iconCls : "icon-metro-checkbox-unchecked",
				disabled : false,
				handler : function() {
					unCheckAll($tree);
				}
			} ]

		});
		$("#add").click(function() {
			addChild();
		});
		$("#addRoot").click(function() {
			//新增根节点
			addRoot();
		});
		$("#delete").click(function() {
			multiDeleteTreeNode($tree, "${ctx}/fnd/org/delete/");
		});
		$("#save").click(function() {
			$("#fm").form("submit", {
				url : "${ctx}/fnd/org/update",
				success : function(data) {
					var node = $tree.tree('getSelected');
					if (node) {
						var fmData = $("#fm").form("getData");
						$tree.tree('update', {
							target : node.target,
							text : fmData.orgNo + " -- " + fmData.orgName,
							attributes : fmData
						});
						$tree.tree("select", node.target);
					}
					$.messager.show({
						msg : data.msg,
						icon : "info",
						position : "bottomRight"
					});
				}
			});
		});
		$("#import").click(function() {
			$.easyui.importor.showImportor({
				importNo : "PBS",
				onExportSuccess : function(file, data) {
					$tree.tree('load');
				}
			});
		});
		$("#excelExport").click(function() {
			var url = "${ctx}/fnd/org/excelExport" ;
			window.parent.location.href = url;
		});
	});

	function addChild() {
		var node = $tree.tree("getSelected");
		if (node == null) {
			$.messager.alert("操作提醒", "请先选中节点", "warning");
			return false;
		}
		var rootOrgId="";
		if(node.attributes.rootOrgId){
			rootOrgId=node.attributes.rootOrgId;
		}else{
			rootOrgId=node.id;
		}
		
		var orgLevel=node.attributes.orgLevel*1+1;
		
		$.post("${ctx}/fnd/org/update", {
			parentOrgId : node.id,
			rootOrgId:rootOrgId,
			orgLevel:orgLevel
		}, function(data) {
			if (data.success) {
				$tree.tree('append', {
					parent : node.target,
					data : [ {
						id : data.obj.id,
						text : data.obj.text,
						attributes : $.extend(data.obj.attributes, {
							//parentOrgId : node.id,
							//rootOrgId:rootOrgId
						})
					} ]
				});
				$tree.tree("select", $tree.tree('find', data.obj.id).target);
			} else {
				$.messager.show({
					icon : "error",
					msg : data.msg
				});
			}
		}, "json");
	}
	function addRoot() {
		/* var node = $tree.tree("getSelected");
		if (node == null) {
			$.messager.alert("操作提醒", "请先选中节点", "warning");
			return false;
		} */
		
		var orgLevel=1;		
		var node = $tree.tree("getRoot");
		$.post("${ctx}/fnd/org/update", {
			orgLevel : orgLevel
		}, function(data) {
			if (data.success) {
				$tree.tree('insert', {
					after : node.target,
					data : [ {
						id : data.obj.id,
						text : data.obj.text,
						attributes : $.extend(data.obj.attributes, {
							pbsParentNode : node.id
						})
					} ]
				});
				$tree.tree("select", $tree.tree('find', data.obj.id).target);
			} else {
				$.messager.show({
					icon : "error",
					msg : data.msg
				});
			}
		}, "json");
	}
	function splitCode(origin) {
		if (origin.length <= 2) {
			return [ origin, "" ];
		} else {
			return [ origin.substring(0, 2), origin.substring(2) ];
		}
	}

</script>
</head>

<body>
	<div data-options="region:'center',fit:true,border:false">
		<div id="cc" class="easyui-layout" data-options="fit:true" style="width: 680px; height: 440px;">
			<div data-options="region:'center'" style="padding: 1px; witdh: 300px">
				<div id="tt" class="easyui-toolbar">
					<shiro:hasPermission name="org:update">
				<button id="addRoot" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-plus"></span> 新增根节点
				</button>
				<button id="add" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-pencil"></span> 增加子节点
				</button>
			</shiro:hasPermission>
			<shiro:hasPermission name="org:delete">
				<button id="delete" type="button" class="btn btn-danger">
					<span class="glyphicon glyphicon-minus"></span> 删除
				</button>
			</shiro:hasPermission>
				</div>
				<ul id="tree">
				</ul>
			</div>
			<div data-options="region:'east',split: true, collapsible:true" style="padding: 1px; witdh: 600px">
				<shiro:hasPermission name="org:update">
					<div class="easyui-toolbar">
						<button id="save" type="button" class="btn btn-primary">
							<span class="glyphicon glyphicon-pencil"></span>保存
						</button>
					</div>
					<form id="fm" method="post">
						<input type="hidden" name="id" />
						<input type="hidden" name="parentOrgId" />
						<input type="hidden" name="rootOrgId" />
						<input type="hidden" name="orgLevel" />
						<fieldset style="border: solid 0px #aaa; padding: 3px;">
							<legend>编辑</legend>
							<table id="tbl">
								<tr>
									<td><label>机构编码:</label></td>
									<td><input name="orgNo" class="easyui-textbox easyui-validatebox" data-options="required: true"></td>
									<td><label>机构名称:</label></td>
									<td><input name="orgName" class="easyui-textbox easyui-validatebox" data-options="required: true"></td>
								</tr>
								<tr>
									<td><label>电话号码:</label></td>
									<td><input name="phoneNumber" class="easyui-textbox easyui-numberbox" data-options=""></td>
									<td><label>传真:</label></td>
									<td><input name="fax" class="easyui-textbox easyui-validatebox" ></td>
								</tr>
								<tr>
									<td><label>邮编号码:</label></td>
									<td><input name="zipCode" class="easyui-textbox easyui-validatebox" ></td>
									<td><label>地址:</label></td>
									<td><input name="address" class="easyui-textbox easyui-validatebox" ></td>
								</tr>
								<tr>
									<td><label>是否启用:</label></td>
									<td><input id="radio1" name="enable" type="radio" value="1" /><label for="radio1">是</label> <input id="radio2" name="enable"
										type="radio" value="2" /><label for="radio2">否</label></td>
									<td>&nbsp;</td>
									<td>&nbsp;</td>
								</tr>
								<tr>
									<td><label>描述:</label></td>
									<td colspan="3"><textarea name="description" cols="70" rows="3"></textarea></td>
								</tr>
								<tr>
									<td><label>备注:</label></td>
									<td colspan="3"><textarea name="remark" cols="70" rows="3"></textarea></td>
								</tr>
							</table>
						</fieldset>
					</form>
				</shiro:hasPermission>
			</div>
		</div>
	</div>
</body>
</html>
